package com.vf.admin.utils.Base64Image;

import com.aliyun.oss.OSSClient;
import com.vf.admin.utils.StringEx;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;


import java.io.*;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class FileUtil {

    /**
     * 文件上传（base64文件流）
     * @param base base64字节码
     * @param upload 上传文件夹名
     */
    public static Map<String,String> aliyunOSSUpload(String base,String upload){
        Map<String,String> map=new HashMap<>();

        String str=base.substring(0,4);
        if(str.equals("http")){
           map.put("st","0");
           map.put("data",base);
           return map;
        }

        Date date=new Date();
        String path="D://upload//";
        GenerateImage.createDir(path);
        String img = date.getTime()+InvitationCode.getStringRandom(13)+".jpg";
        GenerateImage.getBaseImage(base,path+img);
        File file=new File(path+img);

        OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();

        AliyunOSSClientUtil.uploadObject2OSS(ossClient,file,AliyunOSSClientUtil.BUCKETNAME, upload);
        //保存时间 10年
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
        String url = ossClient.generatePresignedUrl(AliyunOSSClientUtil.BUCKETNAME, upload + img, expiration)+"";

        if (StringEx.isNotNull(url)){
            GenerateImage.deleteFile("D://upload");
            String imgurl=url.substring(0,url.indexOf("?"));
            map.put("st","0");
            map.put("data",imgurl);
        } else {
            GenerateImage.deleteFile("D://upload");
            map.put("st","1");
            map.put("data","上传失败");
        }

        return map;
    }



    public static void main(String[] args) {
        String str="http://vifei-updoor.oss-cn-beijing.aliyuncs.com/shop1578543995021c22492u8ky7c1.jpg";
        str= str.substring(0,4);
        System.out.println("********"+str);
    }

//   /* @RequestMapping("/upOssImage")
//    @ResponseBody
//    public Result upOssImage(@RequestParam(value = "file",required=false)MultipartFile file){
//        Map<String,Object> map = new HashMap<>();
//        String resultStr = null;
//        try{
//            String name = file.getOriginalFilename();
//            String suffix = name.substring(name.indexOf("."));
//            String randomName = UUID.randomUUID().toString().replaceAll("-", "");
//            String fileName = randomName + suffix;
//            Long size = file.getSize();
//            // 创建OSSClient实例
//            OSSClient ossClient = new OSSClient(AliyunOSSClientUtil.ENDPOINT, AliyunOSSClientUtil.ACCESSKEYID, AliyunOSSClientUtil.ACCESSKEYSECRET);
//            // 上传：picturePath + uploadFile.getOriginalFilename() --> key
//            ossClient.putObject(AliyunOSSClientUtil.BUCKETNAME, AliyunOSSClientUtil.KEY + fileName, new ByteArrayInputStream(file.getBytes()));
//            // 关闭client
//            ossClient.shutdown();
//            //设置过期时间 -- 十年
//            Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
//            //取出文件上传路径
//            String url = ossClient.generatePresignedUrl(AliyunOSSClientUtil.BUCKETNAME, AliyunOSSClientUtil.KEY + fileName, expiration).toString();
//            String aaa = url.substring(0,url.indexOf("?"));
//            Map<String,String> mm = new HashMap<>();
//            AppSetup byId = appSetupService.getById(1);
//            String pictureprefix = byId.getPictureprefix();
//            mm.put("pictureprefix",pictureprefix);
//            mm.put("name",aaa);
//           *//* map.put("code",1);
//            map.put("msg","上传成功");
//            map.put("data",mm);*//*
//            return sucess(mm);
//        }catch (Exception e){
//          *//*  map.put("code",1);
//            map.put("msg","上传失败");*//*
//            return  error("上传失败");
//        }
//    }
//*/

}
